1
/****************************** Module Header ******************************\
2 * Module Name: Program.cs
3 * Project: CSEnumerateAppDomains
4 * Copyright (c) Microsoft Corporation.
6 * This source file is used to handle the input command. If this application
7 * starts with an argument, process the command directly and then exit, else
8 * show the help text to let user choose a command.
10 * This source is subject to the Microsoft Public License.
11 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
12 * All other rights reserved.
14 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
15 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
17 \***************************************************************************/
20 using Microsoft
.Samples
.Debugging
.CorDebug
;
22 namespace CSEnumerateAppDomains
26 static void Main(string[] args
)
29 // Create new AppDomain in current process.
30 AppDomain
.CreateDomain("Hello world!");
34 // If this application starts without any argument, show the help text to
35 // let user choose a command.
38 // Application will not exit until user types the exit command.
39 // If the command is not correct, it will show the help text in the
44 Please choose a command:
45 1: Show AppDomains in current process.
46 2: List all managed processes.
48 4: Exit this application.
49 To show the AppDomains in a specified process, please type ""PID"" and
50 the ID of the process directly, like PID1234.
53 string cmd
= Console
.ReadLine();
55 if (int.TryParse(cmd
, out cmdID
))
60 ProcessCommand("CurrentProcess");
63 ProcessCommand("ListAllManagedProcesses");
70 // Show the help text in the next loop.
75 else if (cmd
.StartsWith("PID", StringComparison
.OrdinalIgnoreCase
))
82 else if (args
.Length
== 1)
84 ProcessCommand(args
[0]);
89 Console
.WriteLine(ex
.Message
);
91 // The exit code 100 means that this application does not run successfully.
92 Environment
.Exit(100);
96 static void ProcessCommand(string arg
)
98 // List AppDomains in current process.
99 if (arg
.Equals("CurrentProcess", StringComparison
.OrdinalIgnoreCase
))
101 Console
.WriteLine("List AppDomains in current process...");
102 ShowAppDomainsInCurrentProcess();
105 // List all managed processes.
106 else if (arg
.Equals("ListAllManagedProcesses", StringComparison
.OrdinalIgnoreCase
))
108 Console
.WriteLine("List all managed processes...");
109 ListAllManagedProcesses();
112 // Show the AppDomains in a specified process, arg must starts with "PID".
113 else if (arg
.StartsWith("PID", StringComparison
.OrdinalIgnoreCase
))
116 int.TryParse(arg
.Substring(3), out pid
);
117 Console
.WriteLine(string.Format(
118 "List AppDomains in the process {0} ...", pid
));
124 throw new ArgumentException("Please type a valid command.");
130 /// Show AppDomains in Current Process.
132 static void ShowAppDomainsInCurrentProcess()
135 // GetAppDomainsInCurrentProcess is a static method of the class ManagedProcess.
136 // This method is used to get all AppDomains in Current Process.
137 var appDomains
= ManagedProcess
.GetAppDomainsInCurrentProcess();
139 foreach (var appDomain
in appDomains
)
141 Console
.WriteLine("AppDomain Id={0}, Name={1}",
142 appDomain
.Id
, appDomain
.FriendlyName
);
147 /// Show AppDomains in a specified process.
149 /// <param name="pid"> The ID of the Process.</param>
150 static void ShowAppDomains(int pid
)
154 throw new ArgumentException("Please type a valid PID.");
157 ManagedProcess process
= null;
160 // GetManagedProcessByID is a static method of the class ManagedProcess.
161 // This method is used to get an instance of ManagedProcessInfo. If there is
162 // no managed process with this PID, an ArgumentException will be thrown.
163 process
= ManagedProcess
.GetManagedProcessByID(pid
);
165 foreach (CorAppDomain appDomain
in process
.AppDomains
)
167 Console
.WriteLine("AppDomain Id={0}, Name={1}",
173 catch (ArgumentException _argumentException
)
175 Console
.WriteLine(_argumentException
.Message
);
177 catch (ApplicationException _applicationException
)
179 Console
.WriteLine(_applicationException
.Message
);
183 Console
.WriteLine(ex
.Message
);
184 Console
.WriteLine(ex
.GetType());
185 Console
.WriteLine(ex
.StackTrace
);
186 Console
.WriteLine("Cannot get the process. "
187 + " Make sure the process exists and it's a managed process");
199 /// List all managed processes.
201 static void ListAllManagedProcesses()
204 // GetManagedProcesses is a static method of the class ManagedProcess.
205 // This method is used to get a list that contains all managed processes
206 // in current machine.
207 var processes
= ManagedProcess
.GetManagedProcesses();
209 foreach (var process
in processes
)
211 Console
.WriteLine("ID={0}\tName={1}",
212 process
.ProcessID
, process
.ProcessName
);
213 Console
.Write("Loaded Runtimes: ");
214 foreach (var runtime
in process
.LoadedRuntimes
)
216 Console
.Write(runtime
.GetVersionString() + "\t");
218 Console
.WriteLine("\n");